﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain;
using Service.Interface;

namespace Clannan.Controllers
{
    public class AreaController : Controller
    {
        public IAreaService AreaService;

        /// <summary>
        /// 为笔记本添加分区
        /// </summary>
        /// <param name="areaID">笔记本分区ID</param>
        /// <param name="areaName">笔记本分区名称</param>
        /// <returns></returns>
        public JsonResult Create(string notebookID, string areaName)
        {
            Area area = new Area();
            area.Name = areaName;
            area.MarkDelete = false;
            area.Share = false;
            area.PageCount = 0;
            area.PageSortType = "CreateTime";
            area.NoteBook = new NoteBook { ID = Convert.ToInt32(notebookID) };
            this.AreaService.Save(area);

            return Json(new { Message = 1, AreaID = area.ID });
        }

        /// <summary>
        /// 将笔记本分区放入垃圾箱
        /// </summary>
        /// <param name="areaid">笔记本分区ID</param>
        /// <returns></returns>
        public JsonResult Throw(string areaID)
        {
            this.AreaService.UpdateAreaMarkDelete(areaID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 从垃圾箱中还原笔记本分区
        /// </summary>
        /// <param name="areaid">笔记本分区ID</param>
        /// <returns></returns>
        public JsonResult Restore(string areaID)
        {
            this.AreaService.UpdateAreaMarkDelete(areaID, false);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 更新笔记本分区名称
        /// </summary>
        /// <param name="areaID">笔记本分区ID</param>
        /// <param name="areaName">笔记本分区名称</param>
        /// <returns></returns>
        public JsonResult Update(string areaID, string areaName)
        {
            Area area = this.AreaService.Get(areaID);
            area.Name = areaName;
            this.AreaService.Update(area);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 更新分区所属笔记本
        /// </summary>
        /// <param name="areaID">分区名称</param>
        /// <param name="notebookID">笔记本ID</param>
        /// <returns></returns>
        public JsonResult UpdateAreaNoteBook(string areaID, string notebookID)
        {
            Area area = this.AreaService.Get(areaID);
            area.NoteBook = new NoteBook { ID = Convert.ToInt32(notebookID) };
            this.AreaService.Save(area);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 右键直删除分区
        /// </summary>
        /// <param name="areaid">笔记本分区ID</param>
        /// <returns></returns>
        public JsonResult Delete(string areaID)
        {
            this.AreaService.Delete(areaID);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 共享笔记本分区,包括共享分区中的页
        /// </summary>
        /// <param name="areaID">笔记本分区ID</param>
        /// <returns></returns>
        public JsonResult Share(string areaID)
        {
            this.AreaService.UpdateAreaShare(areaID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 取消共享笔记本分区
        /// </summary>
        /// <param name="areaID">笔记本分区ID</param>
        /// <returns></returns>
        public JsonResult CancelShare(string areaID)
        {
            this.AreaService.UpdateAreaShare(areaID, false);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 保存分区中页的排列顺序
        /// </summary>
        /// <param name="areaID">分区ID</param>
        /// <param name="pageSort">排序字符串</param>
        /// <param name="pageSortType">排序类型（Custom，CreateTime，UpdateTime）</param>
        /// <returns></returns>
        public JsonResult UpdatePageSortType(string areaID, string pageSort, string pageSortType)
        {
            Area area = this.AreaService.Get(areaID);
            area.PageSort = pageSort;
            area.PageSortType = pageSortType;
            this.AreaService.Save(area);

            return Json(new { Message = 1 });
        }
    }
}
